#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int getMinSteps(int R, int C, vector<vector<char>>& maze) {
	vector<vector<bool>> visited(R, vector<bool>(C, false));
	vector<int> dx = {-1, 0, 1, 0};
	vector<int> dy = {0, 1, 0, -1};
	
	queue<pair<int, int>> que;
	que.push({0, 0});
	visited[0][0] = true;
	
	int steps = 1;
	while (!que.empty()) {
		int size = que.size();
		while (size--) {
			int x = que.front().first;
			int y = que.front().second;
			que.pop();
			
			if (x == R - 1 && y == C - 1) {
				return steps;
			}
			
			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				
				if (nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nx][ny] == '.' && !visited[nx][ny]) {
					que.push({nx, ny});
					visited[nx][ny] = true;
				}
			}
		}
		steps++;
	}
	
	return -1;
}

int main() {
	int R, C;
	cin >> R >> C;
	
	vector<vector<char>> maze(R, vector<char>(C));
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) {
			cin >> maze[i][j];
		}
	}
	
	int result = getMinSteps(R, C, maze);
	cout << result << endl;
	
	return 0;
}
